package com.lyf.shop.services.Impl;

import com.lyf.shop.common.Const;
import com.lyf.shop.dao.CartDao;
import com.lyf.shop.dao.Impl.CartDaoImpl;
import com.lyf.shop.dao.Impl.OrderDaoImpl;
import com.lyf.shop.dao.OrderDao;
import com.lyf.shop.entity.Cart;
import com.lyf.shop.entity.Item;
import com.lyf.shop.entity.Orders;
import com.lyf.shop.services.CartService;
import com.lyf.shop.services.OrderService;
import com.lyf.shop.util.RandomUtils;
import com.lyf.shop.util.StrUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    @Override
    public void createOrder(int aid, int uid) {
        try {
            //1.获取uid和aid
            if(aid>0) {
                //2.创建orders对象， 用于保存订单信息给数据库去
                Orders orders = new Orders();
                //数据封装
                //3.使用RandomUtils工具类生成订单编号，方便我们后面的Item数据转存
                orders.setUid(uid);
                orders.setAid(aid);
                orders.setOtime(new Date());//保存当前时间
                orders.setOstate(0);//新创的订单默认为未支付
                String oid = RandomUtils.createOrderId();
                orders.setOid(oid);
                //4.通过uid来查找购物车所有的信息
                CartDao cartDao = new CartDaoImpl();
                List<Cart> cart = cartDao.findCart(uid);
                //5.Cart转存数据到item,同时计算金额 循环操作item保存oid的值
                Double sumMoney = 0.0;
                List<Item> items = new ArrayList<>();
                for (Cart cart1 : cart) {
                    Item item = new Item();
                    item.setInum(cart1.getCnum());
                    item.setPid(cart1.getProduct().getPid());
                    item.setIcount(cart1.getCcount());
                    sumMoney += cart1.getCcount();//计算订单的总金额
                    item.setOid(oid);
                    items.add(item);
                }
                orders.setOcount(sumMoney);//设置总金额
                //6.去数据库保存orders,在list<Item> 批量新增操作
                orderDao.InsertOrder(orders);//订单新增
                orderDao.insertItem(items);//传入做批量新增
                //7.清空购物车数据
                cartDao.delCart(uid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<Orders> GetOrderList(int uid) {
        try {
            if (uid>0){
                return orderDao.GetOrderList(uid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Item> getItemByOid(String oid) {
        try {
            if (StrUtils.empty(oid)){
                return orderDao.getItemByOid(oid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Orders getOrderByOid(String oid) {
        try {
            if (StrUtils.empty(oid)){
                return orderDao.getOrderByOid(oid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateOrderStateToPaySuccess(String oid) {
        try {
            if(StrUtils.empty(oid)){
                return orderDao.updateOrderStateToPaySuccess(oid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Orders> getAllOrders(String uname, String ostate) {
        try {
            return orderDao.getAllOrders(uname,ostate);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateOrderStateToSend(String oid, int state) {
        try {
            Orders orders = orderDao.getOrderByOid(oid);
            int states = orders.getOstate();
            if (states== Const.OrderConst.ORDER_Pay_SUCCESS) {
                return orderDao.updateOrderStateToSend(oid, state);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
